//
//  DDConstants.m
//  PrivateCloud
//
//  Created by mars on 13-10-19.
//  Copyright (c) 2013年 sarah doo. All rights reserved.
//

#import "DDConstants.h"
#import "DDFolderInfo.h"
#import "DDFileInfo.h"
#import "DDConstants.h"

static DDConstants* _sharedConstants;
static NSMutableArray* photoExt;
static NSMutableArray* musicExt;
static NSMutableArray* videoExt;
static NSMutableArray* fileExt;
static NSMutableArray* serverFolderInfos;
static NSMutableArray* localFolderInfos;

static int serverContactCount;
static int gloalId = 0;

@implementation DDConstants

- (id)init{
    serverFolderInfos = [DDConstants shardServerFolderInfos];
    photoExt = [DDConstants getPhotoExt];
    musicExt = [DDConstants getMusicExt];
    videoExt = [DDConstants getVideoExt];
    fileExt = [DDConstants getFileExt];
    localFolderInfos = [DDConstants shardLocalFolderInfos];
    return self;
}

+(void)setServerContactCount:(int)count{
    serverContactCount = count;
}

+(int)getServerContactCount{
    return serverContactCount;
}

+(void)removeFileFromLocalById:(int)uid{
    DDFileInfo* deleteFileInfo = nil;
    for (DDFolderInfo *folderInfo in localFolderInfos) {
        for (DDFileInfo *fileInfo in [folderInfo files]) {
            if([fileInfo fileId] == uid){
                deleteFileInfo = fileInfo;
                break;
            }
        }
        if(deleteFileInfo != nil){
            [[folderInfo files]removeObject:deleteFileInfo];
            break;
        }
    }
}

+(NSArray *)getLocalFolders{
    return localFolderInfos;
}

+(DDFolderInfo *)getLocalFoldersById:(int)uid{
    return [localFolderInfos objectAtIndex:uid];
}

+(int)getGloalId
{
    return gloalId;
}

+(void)setGloalId:(int)uid{
    gloalId = uid;
}

+(NSString *)getFileInfoType:(NSString *)fileName{
    NSArray* nameArr = [fileName componentsSeparatedByString:@"."];
    NSString* ext = [nameArr objectAtIndex:([nameArr count] - 1)];
    ext = [ext uppercaseString];
    NSString *type=nil;
    if ([photoExt containsObject:ext]) {
        type = CLOUD_PHOTO;
    }
    else if([musicExt containsObject:ext]){
        type = CLOUD_MUSIC;
    }
    else if([videoExt containsObject:ext]){
        type = CLOUD_VIDEO;
    }
    else if([fileExt containsObject:ext]){
        type = CLOUD_FILE;
    }
    return type;
}

+(void)addServerFiles:(NSArray *)fileInfos{
    for (int i = 0; i < [fileInfos count]; i++) {
        [DDConstants addFile:[fileInfos objectAtIndex:i] andCode:SERVER_CODE];
    }
}

+(void)addFiles:(NSArray *)fileInfos andCode:(int) code{
    for (int i = 0; i < [fileInfos count]; i++) {
        [DDConstants addFile:[fileInfos objectAtIndex:i] andCode:code];
    }
}

+(void)removeLocalFolders{
    [[DDConstants shardLocalFolderInfos]removeAllObjects];
}

+(void)removeAllFilesByCode:(int)code{
    
    NSMutableArray* folderInfos = nil;
    if(code == SERVER_CODE){
        folderInfos = [DDConstants shardServerFolderInfos];
    }
    else if(code == LOCAL_CODE){
        folderInfos = [DDConstants shardLocalFolderInfos];
    }
    
    for (DDFolderInfo* folder in folderInfos) {
        [[folder files]removeAllObjects];
        [folder setCount:0];
    }
    
//    [folderInfos removeAllObjects];
    
//    [[[[DDConstants shardServerFolderInfos]objectAtIndex:0]files]removeAllObjects];
//    [[[[DDConstants shardServerFolderInfos]objectAtIndex:1]files]removeAllObjects];
//    [[[[DDConstants shardServerFolderInfos]objectAtIndex:2]files]removeAllObjects];
//    [[[[DDConstants shardServerFolderInfos]objectAtIndex:3]files]removeAllObjects];
}

//code是1的时候代表服务器 code是2的时候代表客户端
+(void)addFile:(DDFileInfo *)fileInfo andCode:(int)code{
    NSString* fileName = [fileInfo fileName];
    fileName = [fileName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSArray* nameArr = [fileName componentsSeparatedByString:@"."];
    NSString* ext = [nameArr objectAtIndex:([nameArr count] - 1)];
    ext = [ext uppercaseString];

    int num = -1;
    if ([[DDConstants getPhotoExt] containsObject:ext]) {
        num = 0;
    }
    else if([[DDConstants getMusicExt] containsObject:ext]){
        num = 1;
    }
    else if([[DDConstants getVideoExt] containsObject:ext]){
        num = 2;
    }
    else if([[DDConstants getFileExt] containsObject:ext]){
        num = 3;
    }
    if (num != -1) {
        if (code == 1) {
//            NSMutableArray* arr = [[[DDConstants shardServerFolderInfos]objectAtIndex:num]files];
//            [arr addObject:fileInfo];
            if ([[[[DDConstants shardServerFolderInfos] objectAtIndex:num]files] containsObject:fileInfo]) {
                return;
            }
            [[[[DDConstants shardServerFolderInfos] objectAtIndex:num] files] addObject:fileInfo];
            int count = [[[DDConstants shardServerFolderInfos]objectAtIndex:num]count];
            count++;
            [[[DDConstants shardServerFolderInfos]objectAtIndex:num]setCount:count];
        }
        else if(code == 2){
            
            for (DDFileInfo* tempInfo in [[[DDConstants shardLocalFolderInfos]objectAtIndex:num]files]) {
                if([[tempInfo fileName]isEqual:[fileInfo fileName]] && [tempInfo fileLength] == [fileInfo fileLength]){
                    return;
                }
                
            }
//            if ([[[[DDConstants shardLocalFolderInfos]objectAtIndex:num]files]containsObject:fileInfo] ) {
//                return;
//            }
            [[[[DDConstants shardLocalFolderInfos]objectAtIndex:num]files] addObject:fileInfo];
            int count = [[[DDConstants shardLocalFolderInfos]objectAtIndex:num]count];
            count++;
            [[[DDConstants shardLocalFolderInfos]objectAtIndex:num]setCount:count];
        }
        
    }
    
}


+(NSMutableArray *)shardServerFolderInfos{
    if (serverFolderInfos == nil) {
        serverFolderInfos = [[NSMutableArray alloc]init];
        DDFolderInfo *photoInfo = [[DDFolderInfo alloc]init];
        DDFolderInfo *musicInfo = [[DDFolderInfo alloc]init];
        DDFolderInfo *videoInfo = [[DDFolderInfo alloc]init];
        DDFolderInfo *fileInfo = [[DDFolderInfo alloc]init];
        [serverFolderInfos addObject:photoInfo];
        [serverFolderInfos addObject:musicInfo];
        [serverFolderInfos addObject:videoInfo];
        [serverFolderInfos addObject:fileInfo];
    }
    return serverFolderInfos;
}

+(NSMutableArray *)shardLocalFolderInfos{
    if (localFolderInfos == nil) {
        localFolderInfos = [[NSMutableArray alloc]init];
        DDFolderInfo *photoInfo = [[DDFolderInfo alloc]init];
        DDFolderInfo *musicInfo = [[DDFolderInfo alloc]init];
        DDFolderInfo *videoInfo = [[DDFolderInfo alloc]init];
        DDFolderInfo *fileInfo = [[DDFolderInfo alloc]init];
        [localFolderInfos addObject:photoInfo];
        [localFolderInfos addObject:musicInfo];
        [localFolderInfos addObject:videoInfo];
        [localFolderInfos addObject:fileInfo];
    }
    return localFolderInfos;
}

+(void)setSharedLocalFolderInfos:(NSMutableArray*)infos{
    [localFolderInfos removeAllObjects];
    [localFolderInfos addObjectsFromArray:infos];
}

+(DDConstants *)sharedConstants{
    if(!_sharedConstants){
        _sharedConstants = [[DDConstants alloc]init];
    }
    return _sharedConstants;
}

+(DDFileInfo *)getServerFileInfoById:(int)uid{
    for (DDFolderInfo *folderInfo in serverFolderInfos) {
        for (DDFileInfo *fileInfo in [folderInfo files]) {
            if([fileInfo fileId] == uid){
                return fileInfo;
            }
        }
    }
    return nil;
}

+(DDFileInfo *)getLocalFileInfoById:(int)uid{
    for (DDFolderInfo *folderInfo in localFolderInfos) {
        for (DDFileInfo *fileInfo in [folderInfo files]) {
            if([fileInfo fileId] == uid){
                return fileInfo;
            }
        }
    }
    return nil;
}

+(NSMutableArray *)getPhotoExt{
    if(photoExt == nil){
        photoExt = [[NSMutableArray alloc]init];
        [photoExt addObject:@"JPEG"];
        [photoExt addObject:@"JPG"];
        [photoExt addObject:@"PNG"];
        [photoExt addObject:@"PNG"];
        [photoExt addObject:@"BMP"];
        [photoExt addObject:@"GIF"];
    }
    return photoExt;
}
+(NSMutableArray *)getMusicExt{
    if(musicExt == nil){
        musicExt = [[NSMutableArray alloc]init];
        [musicExt addObject:@"MP3"];
        [musicExt addObject:@"WMA"];
        [musicExt addObject:@"WAV"];
    }
    return musicExt;
}

+(NSMutableArray *)getVideoExt{
    if(videoExt == nil){
        videoExt = [[NSMutableArray alloc]init];
        [videoExt addObject:@"MPEG"];
        [videoExt addObject:@"AVI"];
        [videoExt addObject:@"MOV"];
        [videoExt addObject:@"WMV"];
        [videoExt addObject:@"3GP"];
        [videoExt addObject:@"MKV"];
        [videoExt addObject:@"FLV"];
        [videoExt addObject:@"RMVB"];
        [videoExt addObject:@"MP4"];
    }
    return videoExt;
}

+(NSMutableArray *)getFileExt{
    if(fileExt == nil){
        fileExt = [[NSMutableArray alloc]init];
        [fileExt addObject:@"PDF"];
        [fileExt addObject:@"TXT"];
        [fileExt addObject:@"DOC"];
        [fileExt addObject:@"PPT"];
        [fileExt addObject:@"XLS"];
        [fileExt addObject:@"XML"];
    }
    return fileExt;
}
@end
